package com.schooldate.model.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.schooldate.base.BaseAction;
import com.schooldate.model.entity.Department;
import com.schooldate.model.entity.School;
import com.schooldate.util.SimpleJsonFilter;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by etund on 2015/3/15 0015.
 */
@Controller
@Scope("prototype")
@SuppressWarnings("all")
public class DepartmentAction extends BaseAction<Department> {

    private String departmentName;

    private String schoolId;
    /**
     * 根据你的学校返回你的学校的学院
     * @return
     * @throws Exception
     */
       public void getYouDepartment() throws Exception{
           if (schoolId !=null&&!"".equals(schoolId.trim())){
               System.out.println("================getYouDepartment===================="+schoolId);
               School school = this.schoolService.getBySchoolName(schoolId);
               departments = this.departmentService.getBySchoolId(school.getSid()+"");
//               object.clear();
               Set<Department> departmentSet = new HashSet<Department>(departments);
               departments = new ArrayList<Department>(departmentSet);
               object.put("code",1);
               object.put("departments",departments);
               SimpleJsonFilter filter = new SimpleJsonFilter("departments");
               result = JSON.toJSONString(object,filter, SerializerFeature.DisableCircularReferenceDetect);
               renderJson(result);
               return;
           }
           object.put("code",0);
           result = JSON.toJSONString(object);
           renderJson(result);
       }

       public void getDepartmentByName() throws Exception{
           if (this.departmentName !=null && !"".equals(this.departmentName.trim())&&this.schoolId !=null && !"".equals(this.schoolId.trim())){
               String[]  names = departmentName.split("");
               School school = this.schoolService.getBySchoolName(schoolId);
               this.departments = this.departmentService.getBydepartmentNames(names,school.getSid());
               object.put("code",1);
               object.put("departments",departments);
               SimpleJsonFilter filter = new SimpleJsonFilter("departments");
               result = JSON.toJSONString(object,filter, SerializerFeature.DisableCircularReferenceDetect);
               renderJson(result);
           }else {
               object.put("code",0);
               result = JSON.toJSONString(result);
               renderJson(result);
           }
       }

    public String getSchoolId() {
        return schoolId;
    }

    public void setSchoolId(String schoolId) {
        this.schoolId = schoolId;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
}
